package com.thermofisher.dsc.amanda.customer.service.impl;

import com.thermofisher.dsc.amanda.common.Query;
import com.thermofisher.dsc.amanda.common.crud.Util;
import com.thermofisher.dsc.amanda.common.crud.service.AbsBaseService;
import com.thermofisher.dsc.amanda.common.util.SessionUtil;
import com.thermofisher.dsc.amanda.customer.mapper.CustomerMapper;
import com.thermofisher.dsc.amanda.customer.mapper.ITSCARMapper;
import com.thermofisher.dsc.amanda.customer.service.CustomerService;
import com.thermofisher.dsc.amanda.model.dto.customer.CustomerDto;
import com.thermofisher.dsc.amanda.model.po.customer.Customer;
import com.thermofisher.dsc.amanda.model.response.PageResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wenjie.yang on 1/3/2019.
 */
@Service
public class CustomerServiceImpl extends
        AbsBaseService<CustomerDto, Customer, CustomerMapper> implements CustomerService {

    private static final List<String>
            DEPARTMENT = Arrays.asList("BID", "LSC", "BPD", "Other");

    /**
     * 默认构造函数
     */
    public CustomerServiceImpl(CustomerMapper mapper) {
        super(mapper);
    }

    @Autowired
    private ITSCARMapper itscarMapper;

    private final String ADVANCE_PAY = "PP";


    @Override
    public PageResponse getList(Query query) {
        PageResponse response = super.getList(query);
        List<CustomerDto> customerDtos = response.getData();
        customerDtos.forEach(c -> setAdvancePay(c));
        return response;
    }

    private void setAdvancePay(CustomerDto c) {
        if (c.getShip2AlphaName().contains(ADVANCE_PAY)) {
            c.setAdvancePay(true);
        }
    }

    @Override
    public PageResponse getCustomers(Query query) {
        int page = query.getPage();
        int length = query.getLength();
        int start = 0;
        int end = 0;
        if (page <= 0) {
            page = 1;
        }
        if (length <= 0) {
            length = 15;
        }
        start = (page - 1) * length;
        end = start + length;
        String key = "";
        if (query.get("ship2NO_OR_ship2AlphaName") != null) {
            key = query.get("ship2NO_OR_ship2AlphaName").toString();
        } else if (query.get("ship2NO") != null) {
            key = query.get("ship2NO").toString();
        }
        List<Customer> customers = mapper.getCustomers(start, end, key, SessionUtil.getUserDetails().getUsername());
        List<CustomerDto> customerDtos = new ArrayList<>();
        Long total = 0L;
        if (!customers.isEmpty()) {
            total = mapper.countCustomers(key, SessionUtil.getUserDetails().getUsername());
            for (Customer customer : customers) {
                CustomerDto customerDto = new CustomerDto();
                BeanUtils.copyProperties(customer, customerDto);
                BigDecimal totalAmount = itscarMapper.getAmount(customerDto.getBill2NO());
                if (totalAmount != null && totalAmount.doubleValue() > 0) {
                    customerDto.setAdvancePay(true);
                }
                customerDtos.add(customerDto);
            }
            return new PageResponse(query.getPage(), query.getLength(), total, null, customerDtos);
        }
        return new PageResponse(query.getPage(), query.getLength(), total, null, customerDtos);
    }

    @Override
    public CustomerDto getBySt(String stNo) {
        Customer customer = new Customer();
        customer.setShip2NO(stNo);
        return Util.convert(mapper.selectOne(customer), CustomerDto.class);
    }
}
